package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.Result;
import com.wms.entity.Menu;
import com.wms.entity.User;
import com.wms.entity.dto.ListPage;
import com.wms.service.MenuService;
import com.wms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wms
 * @since 2022-07-21
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        List list = userService.lambdaQuery()
                .eq(User::getNo, user.getNo())
                .eq(User::getPassword, user.getPassword()).list();
        if (list.size() > 0) {
            User user1 = (User) list.get(0);
            List menuList = menuService.lambdaQuery().like(Menu::getMenuright, user1.getRoleId()).list();
            HashMap res = new HashMap();
            res.put("user", user1);
            res.put("menu", menuList);
            return Result.suc(res);
        }
        return Result.fail();
    }

    //分页查询
    @PostMapping("/listPage")
    public Result listPage(@RequestBody ListPage listPage) {
        IPage<User> iPage = new Page(listPage.getCurrent(), listPage.getPageSize());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (listPage.getUser().getName() != null && !listPage.getUser().getName().equals("")) {
            wrapper.like("name", listPage.getUser().getName());
        }
        if (listPage.getUser().getSex() != null && !listPage.getUser().getSex().equals("")) {
            wrapper.like("sex", listPage.getUser().getSex());
        }
        IPage<User> page = userService.page(iPage, wrapper);
        listPage.getMap().put("list", page.getRecords());//分页数据
        listPage.getMap().put("total", page.getTotal());//总条数
        return Result.suc(listPage);
    }

    //添加管理员
    @PostMapping("/add")
    private Result add(@RequestBody User user) {
        return userService.save(user)?Result.suc(user):Result.fail();
    }

    //修改
    @PostMapping("/up")
    private Result up(@RequestBody User user) {
        return userService.updateById(user)?Result.suc(user):Result.fail();
    }

    //删除
    @GetMapping("/del")
    private Result del(@RequestParam String id) {
        return userService.removeById(id)?Result.suc(id):Result.fail();
    }
    //查询账户是否已存在
    @GetMapping("/selUser")
    private Result selUser(@RequestParam String no){
        Long numUser=userService.lambdaQuery().eq(User::getNo,no).count();
        return numUser>0?Result.suc(numUser):Result.fail();
    }

    @RequestMapping("/hello")
    public Result hello() {
        return Result.suc("1111");
    }

    @PostMapping("/list")
    public Result list(@RequestBody  User user){
        return userService.list(user,user.getPageNum(),user.getPageSize());
    }
    @PostMapping("/insert")
    public Result insert(@RequestBody User user){
        return userService.add(user);
    }
    @GetMapping("/delete")
    public Result delete(int id){
        return userService.delete(id);
    }
    @GetMapping("/select")
    public Result select(@RequestBody User user){
        return userService.select(user);
    }
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.update(user);
    }
    @GetMapping("/findByNo")
    public Result find(String no){
        return userService.findByNo(no);
    }
}